package net.alteiar.ui.editor.tools.params;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSpinner;

import net.alteiar.ui.editor.tools.ImageInfoTool;

public class ToolUi extends JPanel {
	private static final long serialVersionUID = 1L;

	private final JButton btnSelect;
	private final ToolParamsUi panelAdditionnalInfo;

	public ToolUi(String name, ToolParamsUi params) {
		this.setLayout(new FlowLayout());
		this.setBorder(BorderFactory.createTitledBorder(name));

		btnSelect = new JButton(name);
		btnSelect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				select(!isSelected());
			}
		});

		panelAdditionnalInfo = params;
		panelAdditionnalInfo.add(new JSpinner());
		panelAdditionnalInfo.setVisible(false);

		this.add(btnSelect);
		this.add(panelAdditionnalInfo);

		this.setMinimumSize(new Dimension(80, 80));
	}

	public void unselect() {
		select(false);
	}

	public void select() {
		select(true);
	}

	public boolean isSelected() {
		return panelAdditionnalInfo.isVisible();
	}

	public void addActionListener(ActionListener l) {
		btnSelect.addActionListener(l);
	}

	private void select(boolean select) {
		btnSelect.getModel().setPressed(select);
		panelAdditionnalInfo.setVisible(select);
	}

	public ImageInfoTool getTool() {
		return panelAdditionnalInfo.getTool();
	}
}
